/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2004
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 * 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.model.geo.halfEdgeDataStructure.Vertex;
import br.ufmg.dees.insane.model.postp.GeoPostpModel;
import br.ufmg.dees.insane.model.postp.PointModel;
import br.ufmg.dees.insane.model.postp.PostpModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.persistence.PostpPersistence.PostpPersistenceAsXML;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.DesktopPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.PostpViewState;
import br.ufmg.dees.insane.util.IPoint3d;
import br.ufmg.dees.insane.xyplot.xyplot_model.Axis;
import br.ufmg.dees.insane.xyplot.xyplot_model.ISequence;
import br.ufmg.dees.insane.xyplot.xyplot_model.XYPlot;
import br.ufmg.dees.insane.xyplot.xyplot_view.InterfaceXYPlot;
import br.ufmg.dees.insane.xyplot.xyplot_view.XYPlotViewState;

/**
 * A class representing creation dialog on screen.<br>
 * @author  Penna, Samuel & Pitangueira, Roque
 * @see xyplot_view.InterfaceXYPlot
 * @since  april 2005
 */
public class AddGraphicDialog extends JFrame {
	
	/**   */
	private static final long serialVersionUID = 1L;
	
	private DesktopPostp desktop;
	private InterfacePostp postpIntFrame;
	private XYPlotViewState xyPlotViewState;
	private PostpModel postpModel;
	private JList createdSequencesList;
	private JTextField nodeLabelField;
	private JTextField sequenceTitleField;
	private JComboBox xAxisValuesCombo;
	private JComboBox yAxisValuesCombo;
	private JButton newButton;
	private JButton addButton;
	private JButton deleteSequenceButton;
	private JButton modifySequenceButton;
	private JButton viewSequenceButton;
	private JButton clearSelectionButton;
	private JButton okButton;
	private JButton cancelButton;
	private JScrollPane scrollCreate;
	private JPanel createPane = new JPanel();
	private JPanel contentPane; 
	
	private String[] keysValues;
	
	private ArrayList<Vertex> selection = new ArrayList<Vertex>();
	
	private ArrayList<ISequence> sequences = new ArrayList<ISequence>();
	private Axis xAxis;
	private Axis yAxis;
	
	public AddGraphicDialog(DesktopPostp desktop, ViewState vs){
		super("Add Graphics");
		this.desktop = desktop;
		this.postpIntFrame = (InterfacePostp)((DesktopPostp)this.desktop).getDesktop().getSelectedFrame();
		this.xyPlotViewState = new XYPlotViewState();
	}
	
	public void init(){
		this.setTitle("Add Graphics"+" - "+this.postpIntFrame.getPostpModel().getLabel());
		Image ico = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Insane64.gif"));
		this.setIconImage(ico);
		
		// Sets up the create panel
		JPanel sPane = new JPanel();
		sPane.setLayout(new BoxLayout(sPane, BoxLayout.Y_AXIS));
		sPane.add(createPane);
		createPane.setLayout(new GridLayout(5,2,2,2));
		createPane.setMaximumSize(new Dimension(400,125));
		createPane.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("Create Sequence"),
				BorderFactory.createEmptyBorder(2,2,2,2)));
		
		createPane.add(new JLabel(" Node Label"));
		nodeLabelField = new JTextField();
		nodeLabelField.setBackground(Color.WHITE);
		nodeLabelField.setForeground(Color.BLACK);
		nodeLabelField.setEditable(true);
		nodeLabelField.setEnabled(false);
		createPane.add(nodeLabelField);
		
		xAxisValuesCombo = new JComboBox(keysValues);
		createPane.add(new JLabel("X Axis Value:"));
		xAxisValuesCombo.setMaximumSize(new Dimension(150,25));
		xAxisValuesCombo.setPreferredSize(new Dimension(150,25));
		xAxisValuesCombo.setMinimumSize(new Dimension(150,25));
		xAxisValuesCombo.setSize(new Dimension(150,25));  
		xAxisValuesCombo.setEnabled(false);
		createPane.add(xAxisValuesCombo); 
		
		yAxisValuesCombo = new JComboBox(keysValues);
		createPane.add(new JLabel("Y Axis Value:"));
		yAxisValuesCombo.setMaximumSize(new Dimension(150,25));
		yAxisValuesCombo.setPreferredSize(new Dimension(150,25));
		yAxisValuesCombo.setMinimumSize(new Dimension(150,25));
		yAxisValuesCombo.setSize(new Dimension(150,25)); 
		yAxisValuesCombo.setEnabled(false);
		createPane.add(yAxisValuesCombo); 
		
		createPane.add(new JLabel(" Sequence Title:"));
		sequenceTitleField = new JTextField();
		sequenceTitleField.setBackground(Color.WHITE);
		sequenceTitleField.setForeground(Color.BLACK);
		sequenceTitleField.setEditable(true);
		sequenceTitleField.setEnabled(false);
		createPane.add(sequenceTitleField);
		
		//setting the createdSequencesPane
		JPanel createdSequencesPane = new JPanel();
		createdSequencesPane.setSize(new Dimension(500,500));
		createdSequencesPane.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("Sequences List"),
				BorderFactory.createEmptyBorder(2,2,2,2)));
		createdSequencesPane.setLayout(new BoxLayout(createdSequencesPane,BoxLayout.Y_AXIS)); 
		createdSequencesList = new JList(); 
		createdSequencesList.setBackground(Color.BLACK);
		createdSequencesList.setForeground(Color.GREEN);
		createdSequencesList.setVisibleRowCount(5);
		createdSequencesList.setFixedCellHeight(20);
		createdSequencesList.setFixedCellWidth(300);
		createdSequencesList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		createdSequencesList.setAutoscrolls(true);
		scrollCreate = new JScrollPane(createdSequencesList);
		createdSequencesPane.add(scrollCreate);
		
		newButton = new JButton("New...");
		addButton = new JButton("Add");
		modifySequenceButton = new JButton("Modify");
		deleteSequenceButton = new JButton("Delete");
		viewSequenceButton = new JButton("View Selection");
		clearSelectionButton = new JButton("Clear Selection");
		okButton = new JButton("    OK    ");
		cancelButton = new JButton("Cancel");
		
		contentPane = (JPanel)this.getContentPane(); 
		
		scrollCreate.setViewportView(createdSequencesList); 
		
		
		if(this.selection.size()!=0){
			nodeLabelField.setText(null);
			nodeLabelField.setEnabled(true);
			xAxisValuesCombo.setEnabled(true);
			yAxisValuesCombo.setEnabled(true);
			sequenceTitleField.setText(null);
			sequenceTitleField.setEnabled(true);
			createPane.setEnabled(true);
			nodeLabelField.setText(this.selection.get(0).getId());
		}
		
		newButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						nodeLabelField.setText(null);
						nodeLabelField.setEnabled(true);
						xAxisValuesCombo.setEnabled(true);
						yAxisValuesCombo.setEnabled(true);
						sequenceTitleField.setText(null);
						sequenceTitleField.setEnabled(true);
						createPane.setEnabled(true);
					}
				});
		
		addButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						boolean b = createSequence();
						
						if(b){
							nodeLabelField.setEnabled(false);
							xAxisValuesCombo.setEnabled(false);
							yAxisValuesCombo.setEnabled(false);
							sequenceTitleField.setEnabled(false);
							createPane.setEnabled(false);
						}
						
						
					}
				});
		
		modifySequenceButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						if(createdSequencesList.getSelectedValues().length==1){
							ISequence seq = getSequence((String)createdSequencesList.getSelectedValues()[0]);
							nodeLabelField.setText("New Node");
							sequenceTitleField.setText(seq.getLabel());
							
							nodeLabelField.setEnabled(true);
							xAxisValuesCombo.setEnabled(true);
							yAxisValuesCombo.setEnabled(true);
							sequenceTitleField.setEnabled(true);
							createPane.setEnabled(true);
							
						}else{
							modifyErrMessage();
						}
						
					}
				});
		
		deleteSequenceButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						for(int i=0;i<createdSequencesList.getSelectedValues().length;i++){
							removeSequence((String)createdSequencesList.getSelectedValues()[i]);
						}
						setUpSequencesLabelsList();
					}
				});
		
		viewSequenceButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						viewGraphic(createdSequencesList.getSelectedValues());
					}
				});
		
		clearSelectionButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						createdSequencesList.clearSelection();
					}
				});
		
		okButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						//approvedPerform();
						Close(event);
					}
				});
		
		cancelButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						Close(event);
					}
				});
		
		// 
		// Adding components in the contentPane 
		// 
		contentPane.setLayout(null); 
		addComponent(contentPane, createPane, 5,5,310,175); 
		addComponent(contentPane, newButton, 25,185,80,25);
		addComponent(contentPane, addButton, 215,185,80,25);
		addComponent(contentPane, createdSequencesPane, 320,5,240,175);
		addComponent(contentPane, modifySequenceButton, 340,185,80,25);
		addComponent(contentPane, deleteSequenceButton, 460,185,80,25);
		addComponent(contentPane,viewSequenceButton, 580,30,125,25);
		addComponent(contentPane,clearSelectionButton, 580,60,125,25);
		addComponent(contentPane,okButton, 580,185,75,25);
		addComponent(contentPane,cancelButton, 660,185,75,25);
		
		this.setTitle("Graphics Creator"); 
		this.setLocation(new Point(100,100)); 
		this.setSize(new Dimension(755, 255)); 
		this.setResizable(false);
		this.setUpDialogValues();
		this.setAlwaysOnTop(true);
		this.setVisible(true);
		
	} 
	
	public void modifyErrMessage(){
		JOptionPane.showMessageDialog(this,"Select only one sequence!", "Error Message", JOptionPane.ERROR_MESSAGE);
	}
	
	/** Add Component Without a Layout Manager (Absolute Positioning) */ 
	private void addComponent(Container container,Component c,int x,int y,int width,int height) 
	{ 
		c.setBounds(x,y,width,height); 
		container.add(c); 
	} 	
	
	private void setUpDialogValues(){
		
		if(xyPlotViewState.getSequencesViewList().size()!=0){
			for(int i =0;i<xyPlotViewState.getSequencesViewList().size();i++){
				//sequences.add(((SequenceViewState)xyPlotViewState.getSequencesViewList().get(i)).getLabel());
			}
		}
		
	}
	
	public boolean createSequence(){
		//try{
		StepPostp step = new StepPostp();
		PointModel pointModel;
		ArrayList<IPoint3d> pointList = new ArrayList<IPoint3d>();
		ISequence sequence = new ISequence();
		
		boolean pointExist = true;
		
		if(((PostpViewState)postpIntFrame.getView().getViewState()).getLoadViewState().isOnMemoryOpt()){
			
			this.postpModel = this.postpIntFrame.getPostpModel();
			for(int i =0;i<this.postpModel.getStepList().size();i++){
				step = (StepPostp)this.postpModel.getStepList().get(i);
				if(((GeoPostpModel)step.getGeoPostpModelList().get(0)).getPointModel(nodeLabelField.getText())!=null){
					pointModel = ((GeoPostpModel)step.getGeoPostpModelList().get(0)).getPointModel(nodeLabelField.getText());
					IPoint3d seqPoint = new IPoint3d(); 
					
					if(pointModel.getValues().containsKey((String)this.xAxisValuesCombo.getSelectedItem())){
						if((pointModel.getValue((String)this.xAxisValuesCombo.getSelectedItem())) instanceof Double){
							seqPoint.x = (Double)pointModel.getValue((String)this.xAxisValuesCombo.getSelectedItem());
						}else{
							JOptionPane.showMessageDialog(this, "The value must be a number!", "Error Message", JOptionPane.ERROR_MESSAGE);
							return false;
						}
					}
					if(pointModel.getValues().containsKey((String)this.yAxisValuesCombo.getSelectedItem())){
						if((pointModel.getValue((String)this.yAxisValuesCombo.getSelectedItem())) instanceof Double){
							seqPoint.y = (Double)pointModel.getValue((String)this.yAxisValuesCombo.getSelectedItem());
						}else{
							JOptionPane.showMessageDialog(this, "The value must be a number!", "Error Message", JOptionPane.ERROR_MESSAGE);
							return false;
						}
					}
					
					if(step.getStepValues().containsKey((String)this.xAxisValuesCombo.getSelectedItem())){
						if((step.getStepValue((String)this.xAxisValuesCombo.getSelectedItem())) instanceof Double){
							seqPoint.x = step.getStepValue((String)this.xAxisValuesCombo.getSelectedItem());
						}else{
							JOptionPane.showMessageDialog(this, "The value must be a number!", "Error Message", JOptionPane.ERROR_MESSAGE);
							return false;
						}
					}
					if(step.getStepValues().containsKey((String)this.yAxisValuesCombo.getSelectedItem())){
						if((step.getStepValue((String)this.yAxisValuesCombo.getSelectedItem())) instanceof Double){
							seqPoint.y = step.getStepValue((String)this.yAxisValuesCombo.getSelectedItem());
						}else{
							JOptionPane.showMessageDialog(this, "The value must be a number!", "Error Message", JOptionPane.ERROR_MESSAGE);
							return false;
						}
					}
					pointList.add(seqPoint);
				}
			}
			
			if(pointList.size()==0){
				JOptionPane.showMessageDialog(this,"The Node don't exist", "Error Message", JOptionPane.ERROR_MESSAGE);
				pointExist = false;
			}
			
			if(pointExist){
				Rectangle2D.Double minMax = findMinMax(pointList);
				
				IPoint3d inf = new IPoint3d(minMax.getMinX(), minMax.getMinY());
				IPoint3d sup = new IPoint3d(minMax.getMaxX(), minMax.getMaxY());
				
				sequence = new ISequence(sequenceTitleField.getText(),pointList, inf,sup);
			}
			
		}else if(((PostpViewState)postpIntFrame.getView().getViewState()).getLoadViewState().isOnDiskOpt()){
			
			File file = new File(((PostpViewState)postpIntFrame.getView().getViewState()).getLoadViewState().getPath());
			File loadFile = new File(((PostpViewState)postpIntFrame.getView().getViewState()).getLoadViewState().getPath());
			String aux = loadFile.getName();
			String contPath = loadFile.getParent();
			String name = loadFile.getName();
			String contName = loadFile.getName();
			
			int contFiles = 0;
			int cont = 0;
			
			//boolean isValid = true;
			PostpPersistenceAsXML p = new PostpPersistenceAsXML();
			
			DecimalFormat fmt = new DecimalFormat();
			fmt.applyPattern("000");
			
			int indexName = Integer.parseInt(name.substring(name.indexOf("STEP-")+5,name.indexOf("STEP-")+8));
			cont = Integer.parseInt(name.substring(name.indexOf("STEP-")+5,name.indexOf("STEP-")+8));
			//int loadCont = Integer.parseInt(name.substring(name.indexOf("STEP-")+5,name.indexOf("STEP-")+8));
			this.postpModel = this.postpIntFrame.getPostpModel();
			
			while(file.exists()){
				((PostpViewState)postpIntFrame.getView().getViewState()).getLoadViewState().getStepLoaded().add(contPath+File.separator+aux);
				if(((PostpViewState)postpIntFrame.getView().getViewState()).getLoadViewState().getStepGap()==1){
					contFiles++;
					cont++;
				}else{
					contFiles++;
					cont+=(int)((PostpViewState)postpIntFrame.getView().getViewState()).getLoadViewState().getStepGap();
				}
				p.fillInsaneFromFile(file.getPath());
				
				step.getGeoPostpModelList().add((GeoPostpModel)p.getStep().getGeoPostpModelList().get(0));
				step.getGeoPostpModelList().add((GeoPostpModel)p.getStep().getGeoPostpModelList().get(1));
				
				if(((GeoPostpModel)step.getGeoPostpModelList().get(0)).getPointModel(nodeLabelField.getText())!=null){
					pointModel = ((GeoPostpModel)step.getGeoPostpModelList().get(0)).getPointModel(nodeLabelField.getText());
					IPoint3d seqPoint = new IPoint3d(); 
					
					if((pointModel.getValue((String)this.xAxisValuesCombo.getSelectedItem())) instanceof Double){
						seqPoint.x = (Double)pointModel.getValue((String)this.xAxisValuesCombo.getSelectedItem());
					}else{
						JOptionPane.showMessageDialog(this, "The value must be a number!", "Error Message", JOptionPane.ERROR_MESSAGE);
						return false;
					}
					if((pointModel.getValue((String)this.yAxisValuesCombo.getSelectedItem())) instanceof Double){
						seqPoint.y = (Double)pointModel.getValue((String)this.yAxisValuesCombo.getSelectedItem());
					}else{
						JOptionPane.showMessageDialog(this, "The value must be a number!", "Error Message", JOptionPane.ERROR_MESSAGE);
						return false;
					}
					pointList.add(seqPoint);
				}
				aux = contName.replaceAll(fmt.format(indexName)+"",""+fmt.format(cont));
				file = new File(contPath+File.separator+aux);
			}
			
			if(pointList.size()==0){
				JOptionPane.showMessageDialog(this,"The Node don't exist", "Error Message", JOptionPane.ERROR_MESSAGE);
				pointExist = false;
			}
			
			if(pointExist){
				Rectangle2D.Double minMax = findMinMax(pointList);
				
				IPoint3d inf = new IPoint3d(minMax.getMinX(), minMax.getMinY());
				IPoint3d sup = new IPoint3d(minMax.getMaxX(), minMax.getMaxY());
				
				sequence = new ISequence(sequenceTitleField.getText(),pointList, inf,sup);
			}
		}
		
		if(pointExist){
			if(getSequence(sequence.getLabel())==null){
				addSequence(sequence);
				setUpSequencesLabelsList();
			}else if(getSequence(sequence.getLabel())!=null){
				removeSequence(sequence.getLabel());
				addSequence(sequence);
				setUpSequencesLabelsList();
			}
		}
//		
//		} catch (Exception err) {
//		Toolkit.getDefaultToolkit().beep();
//		this.setVisible(false);
//		}
		return true;
	}
	
	public void viewGraphic(Object[] sequencesLabels){
		
		ArrayList<ISequence> sequencesSelection = new ArrayList<ISequence>();
		
		double xMin = 0.0;
		double yMin = 0.0;
		double xMax = 0.0;
		double yMax = 0.0;
		
		
		for(int i=0;i<sequencesLabels.length;i++){
			sequencesSelection.add(getSequence((String)sequencesLabels[i]));
		}
		
		if(createdSequencesList.getSelectedValues().length==0){
			JOptionPane.showMessageDialog(this, "Select a sequence", "Error Message", JOptionPane.ERROR_MESSAGE);
		}else{
			
			for(int i=0;i<sequencesSelection.size();i++){
				Rectangle2D.Double minMax = findMinMax((ArrayList)((ISequence)sequencesSelection.get(i)).getPointsList());
				if(minMax.getMinX() <= xMin)
					xMin = minMax.getMinX();
				if(minMax.getMinY() <= yMin)
					yMin = minMax.getMinY();
				if(minMax.getMaxX()>= xMax)
					xMax = minMax.getMaxX();
				if(minMax.getMaxY() >= yMax)
					yMax =minMax.getMaxY();
			}
			
			//creating Axis
			IPoint3d originX = new IPoint3d(xMin,0);  
			IPoint3d endX= new IPoint3d(xMax,0);
			
			Axis xx = new Axis("X",originX,endX);
			xx.setMainMark( Math.abs(Math.abs(xMax)- Math.abs(xMin))/10  );
			xx.setSecondaryMark(Math.abs(xMin));
			
			IPoint3d originY = new IPoint3d(0, yMin); 
			IPoint3d endY= new IPoint3d(0, yMax);  
			
			Axis yy = new Axis("Y",originY,endY);
			yy.setMainMark(Math.abs(Math.abs(yMax)- Math.abs(yMin))/10);
			yy.setSecondaryMark(Math.abs(yMin));
			
			List<Axis> axis = new ArrayList<Axis>();
			axis.add(xx);
			axis.add(yy);
			
			//Creating a XYPLot
			XYPlot graphicTest= new XYPlot("Graphic", axis, sequencesSelection);
			
			InterfaceXYPlot xyPlotIntFrame = new InterfaceXYPlot();
			xyPlotIntFrame.validate();
			xyPlotIntFrame.show();
			xyPlotIntFrame.setModel(graphicTest);
			
			desktop.addInternalFrame(xyPlotIntFrame);
		}
		
	}
	
	
	public void Close(ActionEvent event){
		this.dispose();
	}
	
	public void approvedPerform(){
		double xMin = 0.0;
		double yMin = 0.0;
		double xMax = 0.0;
		double yMax = 0.0;
		
		for(int i=0;i<sequences.size();i++){
			Rectangle2D.Double minMax = findMinMax((ArrayList)((ISequence)sequences.get(i)).getPointsList());
			if(minMax.getMinX() <= xMin)
				xMin = minMax.getMinX();
			if(minMax.getMinY() <= yMin)
				yMin = minMax.getMinY();
			if(minMax.getMaxX()>= xMax)
				xMax = minMax.getMaxX();
			if(minMax.getMaxY() >= yMax)
				yMax =minMax.getMaxY();
		}
		
		//creating Axis
		IPoint3d originX = new IPoint3d(xMin,0);  
		IPoint3d endX= new IPoint3d(xMax,0);
		
		Axis xx = new Axis("X",originX,endX);
		xx.setMainMark( Math.abs(Math.abs(xMax)- Math.abs(xMin))/10  );
		xx.setSecondaryMark(Math.abs(xMin));
		
		IPoint3d originY = new IPoint3d(0, yMin); 
		IPoint3d endY= new IPoint3d(0, yMax);  
		
		Axis yy = new Axis("Y",originY,endY);
		yy.setMainMark(Math.abs(Math.abs(yMax)- Math.abs(yMin))/10);
		yy.setSecondaryMark(Math.abs(yMin));
		
		List<Axis> axis = new ArrayList<Axis>();
		axis.add(xx);
		axis.add(yy);
		
		//Creating a XYPLot
		XYPlot graphicTest= new XYPlot("Graphic", axis, sequences);
		InterfaceXYPlot xyPlotIntFrame = new InterfaceXYPlot();
		xyPlotIntFrame.validate();
		xyPlotIntFrame.show();
		xyPlotIntFrame.setModel(graphicTest);
		desktop.addInternalFrame(xyPlotIntFrame);
		
	}
	
	public void setUpSequencesLabelsList(){
		ArrayList<String> createdSequences = new ArrayList<String>();
		for(int i=0;i<this.sequences.size();i++){
			createdSequences.add(((ISequence)this.sequences.get(i)).getLabel());
		}
		this.createdSequencesList.setListData(createdSequences.toArray());
		createdSequences.clear();
	}
	
	/** 
	 * The method find the max and minimum to x and y coordinates of the draw's points
	 * @return limits the draw's limits
	 */
	public Rectangle2D.Double findMinMax(ArrayList pointsList) {
		
		double xMin = ((IPoint3d)pointsList.get(0)).x;
		double yMin = ((IPoint3d)pointsList.get(0)).y;
		double xMax = 0.0;
		double yMax = 0.0;
		
		for(int i=0;i<pointsList.size();i++){
			if(((IPoint3d)pointsList.get(i)).x <= xMin)
				xMin = ((IPoint3d)pointsList.get(i)).x;
			if(((IPoint3d)pointsList.get(i)).y <= yMin)
				yMin = ((IPoint3d)pointsList.get(i)).y;
			if(((IPoint3d)pointsList.get(i)).x>= xMax)
				xMax = ((IPoint3d)pointsList.get(i)).x;
			if(((IPoint3d)pointsList.get(i)).y >= yMax)
				yMax = ((IPoint3d)pointsList.get(i)).y;
		}
		
		if(yMax == yMin){
			yMin -= yMin/10;
			yMax += yMax/10;
		}
		if(xMax == xMin){
			xMin -= xMin/10;
			xMax += xMax/10;
		}
		Rectangle2D.Double limits = new Rectangle2D.Double(xMin, yMin, xMax, yMax);
		return limits;
		
	}
	
	public void addSequence(ISequence sequence){
		this.sequences.add(sequence);
	}
	
	public void removeSequence(ISequence sequence){
		this.sequences.remove(sequence);
	}
	
	public void removeSequence(String seqLabel){
		for(int i=0;i<this.sequences.size();i++){
			if(seqLabel.equals((String)((ISequence)this.sequences.get(i)).getLabel())){
				this.sequences.remove(i);
			}
		}
	}
	
	public ISequence getSequence(String seqLabel){
		ISequence sequence;
		for(int i=0;i<this.sequences.size();i++){
			if(seqLabel.equals((String)((ISequence)this.sequences.get(i)).getLabel())){
				sequence = (ISequence)this.sequences.get(i);
				return sequence;
			}
		}
		return	null;
	}
	
	/**
	 * The method return the desktop.
	 * @return Returns The desktop.
	 */
	public DesktopPostp getDesktop() {
		return desktop;
	}
	
	/**
	 * The method set the field desktop from the parameter desktop.
	 * @param desktop The desktop to set.
	 */
	public void setDesktop(DesktopPostp desktop) {
		this.desktop = desktop;
	}
	
	public String[] getKeysValues() {
		return keysValues;
	}
	
	public void setKeysValues(String[] keysValues) {
		this.keysValues = keysValues;
	}
	
	public PostpModel getPostpModel() {
		return postpModel;
	}
	
	public void setPostpModel(PostpModel postpModel) {
		this.postpModel = postpModel;
	}
	
	public JList getCreatedSequencesList() {
		return createdSequencesList;
	}
	
	public void setCreatedSequencesList(JList createdSequencesList) {
		this.createdSequencesList = createdSequencesList;
	}
	
	public Axis getXAxis() {
		return xAxis;
	}
	
	public void setXAxis(Axis axis) {
		xAxis = axis;
	}
	
	public Axis getYAxis() {
		return yAxis;
	}
	
	public void setYAxis(Axis axis) {
		yAxis = axis;
	}

	public ArrayList<Vertex> getSelection() {
		return selection;
	}

	public void setSelection(ArrayList<Vertex> selection) {
		this.selection = selection;
	}
	
//	*********************************************************************************
}
